package javanio.buffer;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Created by IntelliJ IDEA.
 * User: liuliwei
 * Date: 2017-09-20
 * Time: 下午7:52
 * Description: ${description}
 */
public class FileHole {
    public static void main(String[] args) throws IOException {
        File temp = File.createTempFile("joly", null);
        RandomAccessFile randomAccessFile = new RandomAccessFile(temp, "rw");
        FileChannel channel = randomAccessFile.getChannel();

        //create a working buffer

        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(100);
        putData(0, byteBuffer, channel);
        putData(5000000, byteBuffer, channel);
        putData(500000, byteBuffer, channel);
        System.out.println("Wrote temp file" + temp.getPath()+" size:" + channel.size());
        channel.close();
        randomAccessFile.close();
    }

    private static void putData(int position, ByteBuffer byteBuffer, FileChannel channel) throws IOException {
        String string = "*<--location" + position;
        byteBuffer.clear();
        byteBuffer.put(string.getBytes("US-ASCII"));
        byteBuffer.flip();
        channel.position(position);
        channel.write(byteBuffer);


    }
}
